package msqltest;

import java.sql.*;

public class Jiang {
    public static void main(String[] args) {
        //数据库地址
        String JDBC_URL="jdbc:mysql://localhost:3306/myshool";
        //数据库用户名
        String JDBC_USER="root";
        //数据库密码
        String JDBC_PASSWORD="root";
        //获取连接
        Connection conn;
        PreparedStatement pst=null;

        {
            try {
                conn = DriverManager.getConnection(JDBC_URL,JDBC_USER,JDBC_PASSWORD);
                Statement stmt=conn.createStatement();
                //查数据
                ResultSet rs=stmt.executeQuery("select * from emp");
                while(rs.next()){
                    long id=rs.getLong(1);
                    String name=rs.getString(2);
                    String name1=rs.getString(3);
                    System.out.println(id);
                    System.out.println(name);
                    System.out.println(name1);
                }
                //插入数据
                String sql="insert into emp values(9,'蒋健','afsaasfdas')";
                pst=conn.prepareStatement(sql);
                pst.execute();
                //更新数据
                PreparedStatement ps1=conn.prepareStatement("update emp set ename='讲讲' where empid=7");
                ps1.execute();
                //删除数据
                PreparedStatement ps2=conn.prepareStatement("delete from emp where empid=4");
                ps2.execute();
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }
}
